package implementor;

import java.sql.SQLException;

import utility.AppUtilities;

import model.GinItems;

import dao.GinItemsDao;
import dao.IndentItemsDao;
import dao.IndentsDao;
import dbhandelling.Crude;

import dto.IndentItems;
import dto.Indents;

public class IndentImpl extends Crude implements IndentsDao{

	String className=new Indents().getClass().getName();
	@Override
	public void add(Indents indent) throws SQLException, Exception {
		super.insert(indent);
		
	}

	@Override
	public void update(Indents indent) throws Exception {
		super.update(className, "GeneratedId", indent.getGeneratedId()+"", "autoId", indent.getAutoId()+"");
		super.update(className, "GenerationDate", indent.getGenerationDate()+"", "autoId", indent.getAutoId()+"");
		super.update(className, "indentNo", indent.getIndentNo()+"", "autoId", indent.getAutoId()+"");
		super.update(className, "projectId", indent.getProjectId()+"", "autoId", indent.getAutoId()+"");
		super.update(className, "state", indent.getState()+"", "autoId", indent.getAutoId()+"");
		super.update(className, "version", indent.getVersion()+"", "autoId", indent.getAutoId()+"");
		
	}

	@Override
	public void delete(Indents indent) throws Exception {
		super.delete(className, "autoId", indent.getAutoId()+"");
		
	}

	@Override
	public Indents retreive(String conditionField, String ConditionValue)
			throws Exception {
		Indents i=(Indents) super.retreive(className, conditionField, ConditionValue);
		IndentItemsDao indentItems =new IndentItemsImpl();
		IndentItems[] allItems = indentItems.retreiveArray("",i.getAutoId()+"");
		i.setModelindentItems(allItems);
		i.setModelprojectName(AppUtilities.getProjectName(i.getProjectId()));
		i.setModelgeneratedByName(AppUtilities.getGenratedByName(i.getGeneratedId()));
		return i;
	}

	@Override
	public Indents[] retreiveArray(String conditionField, String ConditionValue)
			throws Exception {
		Object[] ind= super.retreiveArrayOnCondition(className, conditionField, ConditionValue);
		Indents[] s=new Indents[ind.length];
		
		for(int i=0;i<s.length;i++)
			s[i]=(Indents) ind[i];
		return s;
	}

	@Override
	public Indents[] retreiveAll() throws Exception {
		Object[] ind= super.retreiveAll(className);
		Indents[] s=new Indents[ind.length];
		
		for(int i=0;i<s.length;i++)
			s[i]=(Indents) ind[i];
		return s;
	}
	
	@Override
	public int getAutoId()
	{
		int x=super.getAutoGenratedKey(className,"autoId");
		return x;
	}

}
